package com.trainoo.service;

import com.trainoo.hystrix.FileUploadServiceHystrixImpl;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 */
@FeignClient(name = "file-service",
        configuration = IFileUploadService.MultipartSupportConfig.class,
        fallback = FileUploadServiceHystrixImpl.class)
public interface IFileUploadService {

    @Configuration
    class MultipartSupportConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(() ->
                    new HttpMessageConverters(new RestTemplate().getMessageConverters())));
        }
    }

    /**
     * 后端上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/xxx/upload", method = RequestMethod.POST,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    void uploadOne(@RequestParam("file") MultipartFile multipartFile, String  value);
}
